package com.tianji.learning.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianji.common.utils.CollUtils;
import com.tianji.common.utils.DateUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.learning.constants.RedisConstants;
import com.tianji.learning.domain.po.PointsRecord;
import com.tianji.learning.domain.vo.PointsStatisticsVO;
import com.tianji.learning.enums.PointsRecordType;
import com.tianji.learning.mapper.PointsRecordMapper;
import com.tianji.learning.service.IPointsRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 学习积分记录，每个月底清零 服务实现类
 * </p>
 *
 * @author WangXinyi
 * @since 2025-01-11
 */
@Service
public class PointsRecordServiceImpl extends ServiceImpl<PointsRecordMapper, PointsRecord> implements IPointsRecordService {


    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 添加积分记录
     *
     * @param userId
     * @param ponits
     * @param type
     */
    @Override
    public void addSignScore(Long userId, int ponits, PointsRecordType type) {
        //判断积分石否有上限
        int maxPoints = type.getMaxPoints();
        if (maxPoints > 0) {
            //有上限
            //需要先获取今日所有的积分
            LocalDate now = LocalDate.now();
            LocalDateTime atStartOfDay = now.atStartOfDay();
            LocalDateTime atedTime = now.atTime(LocalTime.MAX);
            Integer totalPonits = getBaseMapper().getDayPonits(userId, type.getValue(), atStartOfDay, atedTime);
            if (totalPonits >= maxPoints) {
                //已经超过今日最大积分数
                return;
            }
            //没超过
            if (ponits + totalPonits > maxPoints) {
                ponits = maxPoints - totalPonits;
            }
        }

        //没有上限
        PointsRecord pointsRecord = new PointsRecord();
        pointsRecord.setPoints(ponits);
        pointsRecord.setUserId(userId);
        pointsRecord.setType(type);
        save(pointsRecord);

        //将积分增加到zset排行榜结构中
        String key = RedisConstants.POINTS_BOARD_KEY_PREFIX + DateUtils.format(LocalDate.now(), "yyyyMM");
        String value = userId.toString();
        redisTemplate.opsForZSet().incrementScore(key, value, ponits);
    }


    /**
     * 查询今日积分情况
     */
    @Override
    public List<PointsStatisticsVO> queryPointsStatisticsVO() {
        Long userId = UserContext.getUser();
        LocalDate now = LocalDate.now();
        LocalDateTime atStartOfDay = now.atStartOfDay();
        LocalDateTime atedTime = now.atTime(LocalTime.MAX);
        List<PointsRecord> list = getBaseMapper().queryMyPointsToday(userId, atStartOfDay, atedTime);
        if (CollUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        List<PointsStatisticsVO> pointsStatisticsVOS = list.stream().map(x -> {
            PointsStatisticsVO pointsStatisticsVO = new PointsStatisticsVO();
            pointsStatisticsVO.setMaxPoints(x.getType().getMaxPoints());
            pointsStatisticsVO.setType(x.getType().getDesc());
            pointsStatisticsVO.setPoints(x.getPoints());
            return pointsStatisticsVO;
        }).collect(Collectors.toList());
        return pointsStatisticsVOS;
    }
}
